package com.sinping.qh.service.app.user.impl;

import com.sinping.qh.domain.user.UserInfo;
import com.sinping.qh.dto.app.user.UserInfoNoContentDTO;
import com.sinping.qh.repository.mongo.user.UserInfoRepository;
import com.sinping.qh.repository.mongo.user.impl.UserInfoRespositoryImpl;
import com.sinping.qh.service.app.user.IUserInfoService;
import com.sinping.qh.service.redis.RedisService;
import com.sinping.qh.utils.mapper.BeanMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangjin
 * @create 2018/7/2
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {


    @Autowired
    UserInfoRespositoryImpl userInfoRespositoryImpl;

    @Autowired
    UserInfoRepository userInfoRepository;

    @Autowired
    RedisService redisService;




    @Override
    public List<UserInfoNoContentDTO> getCheckUser(String name) {

        List<UserInfoNoContentDTO> userInfoByAppDTOList =new ArrayList<UserInfoNoContentDTO>() {};
        List<UserInfo> userInfoList=null;

        if(StringUtils.isNotBlank(name)){
            userInfoList  = userInfoRepository.findUserInfoByFullNameIsLike(name);

        }else {
             userInfoList = userInfoRepository.findAll();
        }

        for (UserInfo userInfo:userInfoList) {

            UserInfoNoContentDTO checkUserByAppDTO=
                    BeanMapper.map(userInfo,UserInfoNoContentDTO.class);

            userInfoByAppDTOList.add(checkUserByAppDTO);

        }


        return userInfoByAppDTOList;
    }

    @Override
    public String findOne(String token) {
        String userSessionDto=redisService.hgetByUserinfo(token);
        return userSessionDto;
    }


}
